from datetime import datetime, timedelta
from pathlib import Path
from time import sleep

import peewee
from peewee import SqliteDatabase

from emi.util import SqliteUtil
from emi_gather.framework.RealtimeDBThread import RealtimeDBThread
from emi_gather.framework.builder import ChinaMarketM1BarBuilder, ChinaStockTradeTime
from emi_gather.framework.collector import ChinaStockCollector
from emi_gather.model import storage, TickShotModel
from emi_gather.model.impl._DailyDataDB import DailyDataDB
from emi_gather.model.storage import DefaultRealtimeDB, DBDriver



class SimpleDBDriver(DBDriver):
    def __init__(self):
        self.path = Path("/Users/raodongming/Desktop/gitee/emi-realtime-gather/gather-app/china_sub_stock2.db")
    def create_data_base(self,)->peewee.Database:
        return SqliteUtil.getDatabase(self.path, read_only=False)


file_path = "../gather-app/简单采集工程/daily_bar_storage.db"
indexes_desc = (
    (("code", "yy_mm_dd", "token"), True),
)
origin_tick_db = DailyDataDB(SqliteDatabase(file_path),indexes_desc=indexes_desc)

symbol_list = origin_tick_db.get_code_list()

print(f"code_size: {len(symbol_list)}")
print(f"size:{origin_tick_db.size()}")

code = symbol_list[3289]
newest_day = origin_tick_db.get_newest_datetime()


real_db = DefaultRealtimeDB(db_driver=storage.ChinaStockDBDriver(),read_only=False)
print(f"clear real_db")
real_db.tick_db.clear()
real_db.tick_today_db.clear()
real_db.bar_db.clear()


#raise RuntimeError("")

db_thread = RealtimeDBThread(real_db)

print(f"db_thread start")
db_thread.start()
sleep(1)
_start_ = datetime.now()
print(f"---- size1: {real_db.tick_db.size()}")
print(f"---- size2: {real_db.bar_db.size()}")

today = datetime(2022, 4, 29)
collector = ChinaStockCollector()


for index,code in enumerate(symbol_list):
    # if index > 200:
    #     break
    if index % 200 == 0:
        print(f"p : {index}/{len(symbol_list)}")
    daily_tick_datas = origin_tick_db.get_bars(symbol=code, day=newest_day)
    tickshot_list = []
    for old_tick in daily_tick_datas:
        tickshot = TickShotModel()
        tickshot.code = old_tick.code
        tickshot.yy_mm_dd = old_tick.yy_mm_dd
        hhmmss = ChinaStockTradeTime.map_hh_mm_ss(old_tick.time.hour*10000 + old_tick.time.minute*100+old_tick.time.second)
        tickshot.hh_mm = hhmmss/100
        tickshot.source = old_tick.source
        tickshot.market = 1
        tickshot.time = old_tick.time
        tickshot.close = old_tick.close
        tickshot.open = old_tick.open
        tickshot.high = old_tick.high
        tickshot.low = old_tick.low
        tickshot.pre_close = old_tick.pre_close
        tickshot.volume = old_tick.volume
        tickshot.amount = old_tick.amount
        tickshot.level_data = old_tick.level_data
        tickshot.extra = old_tick.extra
        tickshot_list.append(tickshot)
    collector.collect(tickshot_list,db_thread)
    #sleep(1)


db_thread.release()
report.time_take("finish",start=_start_)

print(f"---- tick size2: {real_db.tick_db.size()}")
print(f"---- bar size2: {real_db.bar_db.size()}")
print(f"---- today size2: {real_db.tick_today_db.size()}")




"""
MainEventEngine finished!!,task_size: 0
耗时[finish]: 0:04:49.557841s
---- tick size2: 1127565
---- bar size2: 696726
---- today size2: 3468
"""
